package com.starwish.starwishblog.web.back;


import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.CommentService;
import com.starwish.starwishblog.vo.CommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("admin/comment")
@Api(value = "评论的操作控制器")
public class CommentController {
    @Autowired
    CommentService commentService;

    /*------查询所有子评论--------------------------------------*/
    @GetMapping("/childCommentList")
    @ApiOperation(value = "查询所有子评论!")
    public ResultResponse<PageInfo<CommentVo>> childCommentList(@RequestParam(value = "now",defaultValue = "1") Integer now,
                @RequestParam(value = "size",defaultValue = "3") Integer size, String content,String title,String username,Integer rootId){
        System.out.println("===="+content+username+title+rootId);
        ResultResponse<PageInfo<CommentVo>> result = null;
        try {
            PageInfo<CommentVo> comments = commentService.findChildCommentByPage(now,size,content,username,title,rootId);
            result = new ResultResponse<>(200,"分页查询评论成功!",comments);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"分页查询评论失败!");
        }
        return result;
    }

    /*------查询所有一级根评论--------------------------------------*/
    @GetMapping("/rootCommentList")
    @ApiOperation(value = "查询所有一级根评论!")
    public ResultResponse<PageInfo<CommentVo>> rootCommentList(@RequestParam(value = "now",defaultValue = "1") Integer now,
                @RequestParam(value = "size",defaultValue = "3") Integer size, String content,String title,String username){
        ResultResponse<PageInfo<CommentVo>> result = null;
        try {
            PageInfo<CommentVo> comments = commentService.findRootCommentByPage(now,size,content,username,title);
            result = new ResultResponse<>(200,"分页查询评论成功!",comments);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"分页查询评论失败!");
        }
        return result;
    }

    @PutMapping("/addComment")
    @ApiOperation(value = "添加评论")
    public ResultResponse<Void> addComment(@RequestBody CommentVo commentVo){
        ResultResponse<Void> result = null;
        try {
            int i = commentService.addComment(commentVo);
            result = new ResultResponse<>(200,"添加评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加评论失败");
        }
        return result;
    }

    @GetMapping("/deleteComment")
    @ApiOperation(value = "删除评论")
    public ResultResponse<Void> deleteComment(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null ;
        try {
            int i = commentService.removeComment(id,status);
            result = new ResultResponse<>(200,"删除评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除评论失败");
        }
        return result;
    }

    @PutMapping("/modifyComment")
    @ApiOperation(value = "修改评论")
    public ResultResponse<Void> modifyComment(@RequestBody CommentVo commentVo){
        ResultResponse<Void> result = null;
        try {
            commentVo.setModifyTime(new Date());
            int i = commentService.modifyComment(commentVo);
            result = new ResultResponse<>(200,"修改评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改评论失败");
        }
        return result;
    }



}
